﻿using System;
using System.Timers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ramparts
{
    class GraphicTimer : GameComponent
    {

        // Font to use for debug
        Timer timer;
        SpriteFont timerFont;
        int currentCount = 0;
        int countInterval = 0;
        int stopCount = 0;

        public GraphicTimer()
        {
        timer = new Timer();
        timer.Elapsed += new ElapsedEventHandler( tick );
        timer.Interval = 1000;
        }

        public void tick( object source, ElapsedEventArgs e )
        {
            currentCount += countInterval;
            if (currentCount == stopCount)
                stopTimer();
        }

        public int getTimerCount()
        {
            return currentCount;
        }
       
        public override void Update(GameTime gameTime)
        {
            // int seconds = timer.         
        }

        public void startTimer()
        {
            timer.Start();
        }

        public void stopTimer()
        {
            timer.Stop();
        }


          public GraphicTimer(Vector2 position, SceneManager inSceneManager, int startTime, int countInterval, int stopTime) 
          {
            currentCount = startTime;
            this.countInterval = countInterval;

            timer = new Timer();
            timer.Elapsed += new ElapsedEventHandler(tick);
            timer.Interval = 1000;
      
            sceneManager = inSceneManager;
            timerFont = sceneManager.rampart.Content.Load<SpriteFont>("Content\\Fonts\\TimerFont");
            this.position = position;
            timer.Stop();
        }

        public override void Draw()
        {
            String timerText = "" + currentCount;
            Vector2 fontOrigin = timerFont.MeasureString(timerText) / 2;
            sceneManager.rampart.spriteBatch.DrawString(timerFont, timerText, 
                                                        new Vector2(position.X - (timerFont.MeasureString(timerText).X /2), 
                                                                    position.Y - timerFont.LineSpacing),
                                                        Color.White,
                                                        0, origin, 1.0f, 
                                                        SpriteEffects.None, 0.5f);
        }

    }
}
